<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/9/1
 * Time: 17:53
 */
namespace app\shop\controller;
use think\Db;
class Message extends Base
{
    public function index(){
        extract(input());
        $page = input('param.page');
        $type = input('param.type')?input('param.type'):1;//1全部  2已读  3未读
        $user_id = session('user_id');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $data = array();
        if($type==1){//全部
            $where['n_type'] = 1;
            $where['n_delete'] = ['not like', '%,' . $user_id . ',%'];

            $map['n_type'] = 2;
            $map['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            $map['n_delete'] = ['not like', '%,' . $user_id . ',%'];
            $data = Db::table('up_notice')
                ->where(function ($query) use($where){
                    $query->where($where);
                })
                ->whereOr(function ($query) use($map){
                    $query->where($map);
                })
                ->order('n_time desc')
                ->paginate(50);
            $PageS = $data->render();
            $data = $data->all();
            if($data) {
                foreach ($data as $k => $v) {
                    // 已读/未读
                    $read_user = explode(',', trim($v['n_read'], ','));
                    if (in_array($user_id, $read_user)) {
                        $data[$k]['is_read'] = "已读"; // 已读
                    } else {
                        $data[$k]['is_read'] = "未读"; // 未读
                    }
                }
            }

        }elseif ($type==2){//已读
            $where['n_type'] = 1;
            $where['n_read'] = ['like', '%,' . $user_id . ',%'];
            $where['n_delete'] = ['not like', '%,' . $user_id . ',%'];

            $map['n_type'] = 2;
            $map['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            $map['n_delete'] = ['not like', '%,' . $user_id . ',%'];
            $map['n_read'] = ['like', '%,' . $user_id . ',%'];

            $data = Db::table('up_notice')
                ->where(function ($query) use($where){
                    $query->where($where);
                })
                ->whereOr(function ($query) use($map){
                    $query->where($map);
                })
                ->order('n_time desc')
                ->paginate(50);
            $PageS = $data->render();
            $data = $data->all();
            if($data) {
                foreach ($data as $k => $v) {
                    // 已读/未读
                    $read_user = explode(',', trim($v['n_read'], ','));
                    if (in_array($user_id, $read_user)) {
                        $data[$k]['is_read'] = "已读"; // 已读
                    } else {
                        $data[$k]['is_read'] = "未读"; // 未读
                    }
                }
            }
        }elseif ($type==3){//未读
            $where['n_type'] = 1;
            $where['n_read'] = ['not like', '%,' . $user_id . ',%'];
            $where['n_delete'] = ['not like', '%,' . $user_id . ',%'];

            $map['n_type'] = 2;
            $map['n_user_id'] = ['like', '%,' . $user_id . ',%'];
            $map['n_delete'] = ['not like', '%,' . $user_id . ',%'];
            $map['n_read'] = ['not like', '%,' . $user_id . ',%'];

            $data = Db::table('up_notice')
                ->where(function ($query) use($where){
                    $query->where($where);
                })
                ->whereOr(function ($query) use($map){
                    $query->where($map);
                })
                ->order('n_time desc')
                ->paginate(50);
            $PageS = $data->render();
            $data = $data->all();
            if($data) {
                foreach ($data as $k => $v) {
                    // 已读/未读
                    $read_user = explode(',', trim($v['n_read'], ','));
                    if (in_array($user_id, $read_user)) {
                        $data[$k]['is_read'] = "已读"; // 已读
                    } else {
                        $data[$k]['is_read'] = "未读"; // 未读
                    }
                }
            }
        }
        return $this->fetch ('message/index',['data'=>$data,'pages'=>$pages,'PageS'=>$PageS,'type'=>$type]);
    }

    //标记已读
    public function read(){
        if(request()->isPost()){
            extract(input());
            $n_id = input('n_id');
            $detail = Db::table('up_notice')
                ->field('n_title, n_content, n_read')
                ->where('n_id', $n_id)
                ->find();
            $user_id = session('user_id');
            if($detail){
                // 是否是已读消息
                $is_read = explode(',', trim($detail['n_read'], ','));
                if(!in_array($user_id, $is_read)){ // 不在已读数组中，说明未读，需要加入已读字段
                    $read_user = $detail['n_read'] .$user_id . ',';
                    Db::table('up_notice')->where('n_id', $n_id)->setField('n_read', $read_user);
                }
                return json(['status' => 200, 'msg' => "操作成功"]);
            }else{
                return json(['status' => 200, 'msg' => "操作失败"]);
            }
        }

    }

    public function del(){
        if(request()->isPost()){
            extract(input());
            $n_id = input('n_id');
            $detail = Db::table('up_notice')
                ->where('n_id', $n_id)
                ->find();
            $user_id = session('user_id');
            if($detail){
                // 是否是已读消息
                $is_read = explode(',', trim($detail['n_delete'], ','));
                if(!in_array($user_id, $is_read)){ // 不在已读数组中，说明未读，需要加入已读字段
                    $read_user = $detail['n_delete'] .$user_id . ',';
                    Db::table('up_notice')->where('n_id', $n_id)->setField('n_delete', $read_user);
                }
                return json(['status' => 200, 'msg' => "操作成功"]);
            }else{
                return json(['status' => 100, 'msg' => "操作失败"]);
            }
        }


    }
}